package FinalProject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Random;


public class Omok_Thread extends Thread {

	private int roomNumber = -1; // room number

	private String userName = null; // user name

	private Socket socket; // socket 
	
	private RoomManager rMan;

	// status of ready.
	private boolean ready = false;

	private BufferedReader reader; // input stream

	private PrintWriter writer; // output stream
	
	private Random rnd = new Random(); // for decide stone color

	Omok_Thread(Socket socket, RoomManager rMan) { // constructor

		this.socket = socket;
		this.rMan = rMan;

	}

	Socket getSocket() { // return socket

		return socket;

	}

	int getRoomNumber() { // return room number

		return roomNumber;

	}

	String getUserName() { // return user name

		return userName;

	}

	boolean isReady() { // return ready status

		return ready;

	}

	public void run() {

		try {

			reader = new BufferedReader(

			new InputStreamReader(socket.getInputStream()));

			writer = new PrintWriter(socket.getOutputStream(), true);

			String msg; // message from client

			while ((msg = reader.readLine()) != null) {

				// if msg is start from "[NAME]"

				if (msg.startsWith("[NAME]")) {

					userName = msg.substring(6); // set userName.

				}

				// if msg is start from "[ROOM]"

				else if (msg.startsWith("[ROOM]")) {

					int roomNum = Integer.parseInt(msg.substring(6));

					if (!rMan.isFull(roomNum)) { // if room is not full

						// shows exit user to current room member.
						if (roomNumber != -1)

							rMan.sendToOthers(this, "[EXIT]" + userName);

						// set new room number.
						roomNumber = roomNum;

						// send message to user which is the room can enter.
						writer.println(msg);

						// send user list who in the new room
						writer.println(rMan.getNamesInRoom(roomNumber));

						// notice to other users in new room that enter the user in new room.
						rMan.sendToOthers(this, "[ENTER]" + userName);

					}

					else
						writer.println("[FULL]"); // notice to user that room is full

				}

				// "[STONE]" send to stone position
				else if (roomNumber >= 1 && msg.startsWith("[STONE]"))

					rMan.sendToOthers(this, msg);

				// send conversation to room
				else if (msg.startsWith("[MSG]"))

					rMan.sendToRoom(roomNumber,

					"[" + userName + "]: " + msg.substring(5));

				// "[START]"

				else if (msg.startsWith("[START]")) {

					ready = true; // ready for start game.

					// if other user ready to start
					if (rMan.isReady(roomNumber)) {

						// decide stone color and send result
						int a = rnd.nextInt(2);

						if (a == 0) {

							writer.println("[COLOR]BLACK");

							rMan.sendToOthers(this, "[COLOR]WHITE");

						}

						else {

							writer.println("[COLOR]WHITE");

							rMan.sendToOthers(this, "[COLOR]BLACK");

						}

					}

				}

				// if user send message which stop game

				else if (msg.startsWith("[STOPGAME]"))

					ready = false;

				// if user send message which give up

				else if (msg.startsWith("[DROPGAME]")) {

					ready = false;

					// notice that opponent gave up.
					rMan.sendToOthers(this, "[DROPGAME]");

				}

				// if user message is win

				else if (msg.startsWith("[WIN]")) {

					ready = false;

					// send message to user which is win.
					writer.println("[WIN]");

					// send message to opponent which is lose.
					rMan.sendToOthers(this, "[LOSE]");

				}

			}

		} catch (Exception e) {

		} finally {

			try {

				rMan.remove(this);

				if (reader != null)
					reader.close();

				if (writer != null)
					writer.close();

				if (socket != null)
					socket.close();

				reader = null;
				writer = null;
				socket = null;

				System.out.println(userName + " Disconnected from server.");

				System.out.println("Number of connected users: " + rMan.size());

				// notice to room that user close the connection
				rMan.sendToRoom(roomNumber, "[DISCONNECT]" + userName);

			} catch (Exception e) {
			}
		}
	}
}
